//
//  LocalLunBo.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/25.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "LocalLunBo.h"
#import "baseCollection.h"
#import "LunBoCollectionCell.h"
#import "LunBoLayout.h"
#import <UIImageView+WebCache.h>
@interface LocalLunBo ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) LunBoLayout *layout;
@property (nonatomic, retain) NSArray *imgArray;
@property (nonatomic, retain)baseCollection *collection;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSTimer *timer;
@end
@implementation LocalLunBo

#pragma mark - ------------ 初始化 ---------------

- (instancetype)initWithFrame:(CGRect)frame imgArr:(NSArray *)imgArray selectImageBlock:(void (^)(NSInteger))selectImageBlock{

    self = [super initWithFrame:frame];
    if (self) {
        self.selectBlock = selectImageBlock;
        self.imgArray = imgArray;
        [self.collection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:25] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        [self createCollection];
        [self creatPageControl];
        
        [self addTimer];
    }
    return self;
}

#pragma mark - ------------ 创建collection ---------------
-(void)createCollection{
 
    
    self.layout = [[LunBoLayout alloc]init];
    self.collection = [[baseCollection  alloc]initWithFrame:self.bounds collectionViewLayout:_layout];
    self.collection.backgroundColor = [UIColor whiteColor];
    [self addSubview:_collection];
    _collection.delegate = self;
    _collection.dataSource = self;
    //分页效果
    
    _collection.pagingEnabled = YES;
    [_collection registerClass:[LunBoCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    [_layout release];
    [_collection release];
     
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 50;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _imgArray.count;
    
}


#pragma mark - ------------ 赋值 ---------------

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    LunBoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:_imgArray[indexPath.item]]];
    return cell;

}
#pragma mark - ------------ 加定时器 ---------------
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(roll) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
}

#pragma mark - ------------ 计时器方法 无限滚动分区里的item ---------------
- (void)roll
{
    
    
    NSIndexPath *path = [[_collection indexPathsForVisibleItems]lastObject];
    NSIndexPath *currentPath = [NSIndexPath indexPathForItem:path.item inSection:50 / 2];
    
    [_collection scrollToItemAtIndexPath:currentPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    
    NSInteger nextItem = currentPath.item + 1;
    NSInteger nextSection = currentPath.section;
    if (nextItem == _imgArray.count) {
        
        nextItem = 0;
        nextSection ++;
        
    }
    
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    
    [_collection scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
    
}


#pragma mark - ------------ 创建page ---------------
- (void)creatPageControl
{
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 180,[UIScreen mainScreen].bounds.size.width, 20)];
    _pageControl.numberOfPages = _imgArray.count;
    _pageControl.currentPage = 0;
    //    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    //    _pageControl.pageIndicatorTintColor = [UIColor yellowColor];
    
    [self addSubview:_pageControl];
    
}

#pragma mark - ------------ 点跟着动 ---------------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5 ) % _imgArray.count;
    
}

#pragma mark - ------------ 点击 ---------------
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  
    self.selectBlock(indexPath.item);

}


@end
